<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>非阻塞I/O模型 - 深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        /* 首字下沉效果 */
        .drop-cap::first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #4f46e5;
            font-family: 'Noto Serif SC', serif;
        }
        
        /* 悬挂标点 */
        .hanging-punctuation {
            hanging-punctuation: first last;
        }
        
        /* 卡片悬停效果 */
        .card-hover {
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }
        
        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        
        /* 渐变文字效果 */
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        /* 代码块样式 */
        .code-inline {
            background: rgba(79, 70, 229, 0.1);
            padding: 0.2rem 0.4rem;
            border-radius: 0.25rem;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 0.9em;
            color: #4f46e5;
        }
        
        /* 滚动动画 */
        .fade-in {
            opacity: 0;
            transform: translateY(20px);
            animation: fadeIn 0.8s ease-out forwards;
        }
        
        @keyframes fadeIn {
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        /* 时间线样式 */
        .timeline-dot {
            position: relative;
        }
        
        .timeline-dot::before {
            content: '';
            position: absolute;
            width: 12px;
            height: 12px;
            background: #4f46e5;
            border-radius: 50%;
            left: -6px;
            top: 50%;
            transform: translateY(-50%);
            box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.2);
        }
        
        /* Mermaid 图表样式 */
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 1rem;
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body>
    <!-- Hero 区域 -->
    <section class="relative overflow-hidden bg-gradient-to-br from-indigo-600 via-purple-600 to-pink-500 text-white">
        <div class="absolute inset-0 bg-black opacity-20"></div>
        <div class="relative container mx-auto px-6 py-24 text-center">
            <h1 class="text-5xl md:text-7xl font-bold mb-6 fade-in">
                非阻塞I/O模型
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90 fade-in" style="animation-delay: 0.2s">
                <i class="fas fa-bolt mr-2"></i>
                突破性能瓶颈，构建高并发系统的核心技术
            </p>
            <div class="flex justify-center space-x-8 text-lg fade-in" style="animation-delay: 0.4s">
                <div class="flex items-center">
                    <i class="fas fa-rocket mr-2"></i>
                    <span>高性能</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-layer-group mr-2"></i>
                    <span>高并发</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-tachometer-alt mr-2"></i>
                    <span>低延迟</span>
                </div>
            </div>
        </div>
        <div class="absolute bottom-0 left-0 right-0">
            <svg viewBox="0 0 1440 120" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M0 120L60 110C120 100 240 80 360 70C480 60 600 60 720 65C840 70 960 80 1080 85C1200 90 1320 90 1380 90L1440 90V120H1380C1320 120 1200 120 1080 120C960 120 840 120 720 120C600 120 480 120 360 120C240 120 120 120 60 120H0V120Z" fill="#f5f7fa"/>
            </svg>
        </div>
    </section>

    <!-- 主要内容区域 -->
    <main class="container mx-auto px-6 py-12 max-w-6xl">
        
        <!-- 引言卡片 -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700 drop-cap hanging-punctuation">
                非阻塞I/O（Non-blocking I/O）是一种革命性的I/O操作方式，它与传统的阻塞I/O（Blocking I/O）形成鲜明对比。在传统的阻塞I/O模型中，当程序发起一个I/O请求时，程序会被挂起直到I/O操作完成，导致线程在等待I/O完成时无法做其他工作。而非阻塞I/O则采用不同的机制，当数据未准备好时，它会立即返回，允许程序继续执行其他任务。
            </p>
        </div>

        <!-- 核心概念区块 -->
        <section class="mb-16">
            <h2 class="text-4xl font-bold mb-8 gradient-text">
                <i class="fas fa-cube mr-3"></i>核心概念
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <!-- 阻塞I/O卡片 -->
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover border-l-4 border-red-500">
                    <h3 class="text-2xl font-bold mb-4 text-gray-800">
                        <i class="fas fa-stop-circle text-red-500 mr-2"></i>阻塞I/O
                    </h3>
                    <p class="text-gray-600 mb-4">
                        当程序发起I/O请求时，线程会被挂起，直到操作系统完成该操作（如从磁盘或网络读取数据）。在此期间，线程处于等待状态，不做任何其他工作。
                    </p>
                    <div class="bg-red-50 rounded-lg p-4">
                        <p class="text-sm text-red-700">
                            <i class="fas fa-exclamation-triangle mr-2"></i>
                            线程被阻塞，资源利用率低
                        </p>
                    </div>
                </div>
                
                <!-- 非阻塞I/O卡片 -->
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover border-l-4 border-green-500">
                    <h3 class="text-2xl font-bold mb-4 text-gray-800">
                        <i class="fas fa-play-circle text-green-500 mr-2"></i>非阻塞I/O
                    </h3>
                    <p class="text-gray-600 mb-4">
                        程序在发起I/O请求时，如果数据未准备好，线程会立即返回，不会被挂起。通过返回特定的错误代码（如<span class="code-inline">EAGAIN</span>或<span class="code-inline">EWOULDBLOCK</span>）通知程序。
                    </p>
                    <div class="bg-green-50 rounded-lg p-4">
                        <p class="text-sm text-green-700">
                            <i class="fas fa-check-circle mr-2"></i>
                            线程继续执行，提高并发能力
                        </p>
                    </div>
                </div>
            </div>

            <!-- 特点展示 -->
            <div class="bg-gradient-to-r from-purple-50 to-indigo-50 rounded-2xl p-8">
                <h3 class="text-2xl font-bold mb-6 text-gray-800">
                    <i class="fas fa-star mr-2 text-yellow-500"></i>非阻塞I/O的特点
                </h3>
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="bg-white rounded-lg p-6 text-center card-hover">
                        <i class="fas fa-unlock text-4xl text-indigo-500 mb-4"></i>
                        <h4 class="font-bold text-lg mb-2">不阻塞线程</h4>
                        <p class="text-gray-600 text-sm">线程可以在等待数据的同时处理其他任务</p>
                    </div>
                    <div class="bg-white rounded-lg p-6 text-center card-hover">
                        <i class="fas fa-network-wired text-4xl text-purple-500 mb-4"></i>
                        <h4 class="font-bold text-lg mb-2">提高并发性</h4>
                        <p class="text-gray-600 text-sm">提高系统的并发性，减少资源浪费</p>
                    </div>
                    <div class="bg-white rounded-lg p-6 text-center card-hover">
                        <i class="fas fa-sync-alt text-4xl text-pink-500 mb-4"></i>
                        <h4 class="font-bold text-lg mb-2">异步特性</h4>
                        <p class="text-gray-600 text-sm">及时响应多路I/O事件，无需独立线程</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- 工作机制可视化 -->
        <section class="mb-16">
            <h2 class="text-4xl font-bold mb-8 gradient-text">
                <i class="fas fa-cogs mr-3"></i>工作机制
            </h2>
            
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-8">
                <div class="mermaid">
                    graph TD
                        A[应用程序] -->|发起I/O请求| B{数据是否准备好?}
                        B -->|是| C[立即返回数据]
                        B -->|否| D[返回EAGAIN/EWOULDBLOCK]
                        D --> E[继续执行其他任务]
                        E --> F[事件循环监控]
                        F --> G{I/O事件就绪?}
                        G -->|是| H[处理I/O事件]
                        G -->|否| F
                        H --> I[完成处理]
                        
                        style A fill:#667eea,stroke:#5a67d8,color:#fff
                        style C fill:#48bb78,stroke:#38a169,color:#fff
                        style D fill:#f56565,stroke:#e53e3e,color:#fff
                        style H fill:#48bb78,stroke:#38a169,color:#fff
                </div>
            </div>

            <!-- 工作步骤时间线 -->
            <div class="bg-gray-50 rounded-2xl p-8">
                <h3 class="text-2xl font-bold mb-6 text-gray-800">
                    <i class="fas fa-list-ol mr-2"></i>工作步骤
                </h3>
                <div class="space-y-6 border-l-2 border-indigo-300 pl-8 ml-4">
                    <div class="timeline-dot">
                        <h4 class="font-bold text-lg mb-2">1. I/O操作的请求</h4>
                        <p class="text-gray-600">当程序请求I/O操作时，操作会被标记为非阻塞。数据未准备好时，立即返回错误代码。</p>
                    </div>
                    <div class="timeline-dot">
                        <h4 class="font-bold text-lg mb-2">2. 事件通知机制</h4>
                        <p class="text-gray-600">通过I/O复用技术（如<span class="code-inline">epoll</span>、<span class="code-inline">select</span>、<span class="code-inline">kqueue</span>），监控多个文件描述符。</p>
                    </div>
                    <div class="timeline-dot">
                        <h4 class="font-bold text-lg mb-2">3.